package com.heima.user.service.impl;

import com.heima.common.cache.CacheService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.user.service.UserRelationService;
import com.heima.user.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class UserRelationServiceimpl implements UserRelationService {
    private CacheService cacheService;
    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //0.参数判空
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //1.获取入参
        Integer authorId = dto.getAuthorId();
        Short operation = dto.getOperation();//0关注 1取关
        Integer userId = AppThreadLocalUtil.getUser().getId();

        if(userId==null||userId==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //2.判断操作类型
        if(operation==0){
            //2.1关注
            cacheService.zAdd(BehaviorConstants.APUSER_FOLLOW_RELATION+userId,authorId.toString(),System.currentTimeMillis());
            cacheService.zAdd(BehaviorConstants.APUSER_FANS_RELATION+authorId,userId.toString(),System.currentTimeMillis());
        }else  if(operation==1){
            //2.2取消关注
            cacheService.zRemove(BehaviorConstants.APUSER_FOLLOW_RELATION+userId,authorId.toString());
            cacheService.zRemove(BehaviorConstants.APUSER_FANS_RELATION+authorId,userId.toString());
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
